package com.cn.xiaonuo.slbfinances.payconfig.service.impl;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cn.xiaonuo.common.constant.Status;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.payconfig.entity.SlPayChannel;
import com.cn.xiaonuo.slbfinances.payconfig.entity.SlPayType;
import com.cn.xiaonuo.slbfinances.payconfig.enums.SlbEnum;
import com.cn.xiaonuo.slbfinances.payconfig.mapper.SlPayChannelMapper;
import com.cn.xiaonuo.slbfinances.payconfig.mapper.SlPayTypeMapper;
import com.cn.xiaonuo.slbfinances.payconfig.param.SlPayTypeParam;
import com.cn.xiaonuo.slbfinances.payconfig.service.SlbPayTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**   
 *  
 * @Description:  支付方式——SERVICEIMPL
 * @Author:       likui
 * @CreateDate:   PayType
 * @Version:      V1.0
 *    
 */
@Service
public class SlbPayTypeServiceImpl implements SlbPayTypeService {
	@Autowired
	private SlPayTypeMapper slPayTypeMapper;

	@Autowired
	private SlPayChannelMapper slPayChannelMapper;
	/**
	 * 支付方式列表
	 * @param param
	 * @return
	 */
	@Override
	@DataSource(name = "jyb_soole_data_hn")
	public BusinessMessage pagePayType(SlPayTypeParam param) {
		BusinessMessage message = new BusinessMessage();
		Integer pageNo=param.getPageNo();
		Integer pageSize=param.getPageSize();
		if (pageNo==null || pageNo.intValue()<1){
			pageNo=1;
		}
		if (pageSize==null || pageSize.intValue()<1){
			pageSize=10;
		}
		PageHelper.startPage(pageNo,pageSize);
		LambdaQueryWrapper<SlPayType> lambdaQueryWrapper=new LambdaQueryWrapper<SlPayType>();
		lambdaQueryWrapper.eq(SlPayType::getDeleted, SlbEnum.DELETED_OK.getCode());
		List<SlPayType> payTypes = slPayTypeMapper.selectList(lambdaQueryWrapper);
		PageInfo<SlPayType> pageInfo = new PageInfo<>(payTypes);
		message.setData(pageInfo);
		message.setMessage(Status.success);
		message.setSuccess(true);
		message.setCode(Status.successCode);
		return message;
	}

	/**
	 * 支付方式现实状态修改
	 * @param param
	 * @return
	 */
	@Override
	@DataSource(name = "jyb_soole_data_hn")
	public BusinessMessage updateState(SlPayTypeParam param) {
		BusinessMessage message = new BusinessMessage();
		if (ObjectUtils.isEmpty(param.getId())){
			message.setMessage("支付方式Id不能为空");
			message.setSuccess(false);
			message.setCode(Status.failCode);
			return message;
		}
		if (ObjectUtils.isEmpty(param.getStatus())){
			message.setMessage("状态不能为空");
			message.setSuccess(false);
			message.setCode(Status.failCode);
			return message;
		}
		SlPayType payType = slPayTypeMapper.selectById(param.getId());
		if (ObjectUtils.isNotEmpty(payType)){
			if (!payType.getId().equals(7) && !payType.getId().equals(8)){
				if (param.getStatus().equals(SlbEnum.STATUS_OK.getCode())){
					LambdaQueryWrapper<SlPayChannel> wrapper = new LambdaQueryWrapper<>();
					wrapper.eq(SlPayChannel::getPayTypeId,param.getId());
					wrapper.eq(SlPayChannel::getDeleted,SlbEnum.DELETED_OK.getCode());
					wrapper.eq(SlPayChannel::getStatus,SlbEnum.STATUS_OK.getCode());
					List<SlPayChannel> payChannels = slPayChannelMapper.selectList(wrapper);
					if (payChannels.size()<1){
						message.setMessage("内部暂无可使用渠道，不可修改!");
						message.setSuccess(false);
						message.setCode(Status.failCode);
						return message;
					}
				}
			}
			payType.setStatus(param.getStatus());
			slPayTypeMapper.updateById(payType);

		}
		message.setMessage(Status.success);
		message.setSuccess(true);
		message.setCode(Status.successCode);
		return message;
	}
}